const network_utils = require('../../../../../../http/NetworkUtils.js');
const constants = require('../../../../../../common/Constants.js');
const umfUtils = require('../../../../../../utils/UmfUtils.js');
var app = getApp()
let storeDesc = "";

const PayTypeWeiXin = "1";
const PayTypeAlipay = "2";
const PayTypeWePub = "3";
const PayAllMaps = {
    [PayTypeWeiXin]: "立马付-微信",
    [PayTypeAlipay]: "立马付-支付宝",
    [PayTypeWePub]: "微信公众号支付"
};

Page({
  data: {
    sectionArray1: _getOrderInfoSection1Arr(),
    sectionArray2: _getOrderInfoSection2Arr(),
  },

  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    wx.setNavigationBarTitle({
      title: ("订单详情")
    });
    let params={}
    let optionsData = JSON.parse(options.params);
    storeDesc = optionsData.title;
    if (optionsData.productId == "1"){
      params = {
        tradeNo: optionsData.tradeNo,
        orderDate: optionsData.orderDate, 
        storeId: optionsData.storeId
      };
      this.requestPosDetail(params)
    }else if(options.tradeNo){
      params = {
        tradeNo: optionsData.tradeNo,
      };
      let that = this;
      network_utils.buildRequest(constants.Interfaces.FUNC_Merchant_QueryOrder, params, {
        doHttpSucess: function (data) {
          if (constants.RetCodes.SUCCESS === data.retCode) {
            that.setData({
              sectionArray1: _getOrderInfoSection1Arr(data),
              sectionArray2: _getOrderInfoSection2Arr(data)
            });
            return true;
          }
          return false;

        }
      }).send();
    }
  },
  //请求POS 交易详情
  requestPosDetail:function(params){
    let that = this;
    network_utils.buildRequest(constants.Interfaces.FUNC_POS_TRADDETAIL, params, {
      doHttpSucess: function (data) {
        if (constants.RetCodes.SUCCESS === data.retCode) {
          that.setData({
            sectionArray1: _getPOSOrderInfoSection1Arr(data),
            sectionArray2: _getPOSOrderInfoSection2Arr(data)
          });
          return true;
        }
        return false;

      }
    }).send();
  }
});

function _getOrderInfoSection1Arr(result) {
  let arr = [];
  arr.push({ title: "交易金额(元)", content: result && result["orderAmount"] ? "+" + umfUtils.fentoyuan(result["orderAmount"]) : "" });
  return arr;
}

function _getOrderInfoSection2Arr(result) {
  let arr = [];
  arr.push({ title: "交易时间", content: result && result["tradeDateTime"] ? result["tradeDateTime"] : "" });
  arr.push({ title: "付款方式", content: result && result["payType"] ? PayAllMaps[result["payType"]]: "" });
  arr.push({ title: "付款人", content: result && result["payer"] ? result["payer"] : "" });
  arr.push({ title: "交易单号", content: result && result["tradeNo"] ? result["tradeNo"] : "" });
  arr.push({ title: "交易门店", content: result ? storeDesc : storeDesc });
  return arr;
  arr.push({ title: "退费次数", content: result ? storeDesc : storeDesc });
  return arr;//补上下两个字段名
  arr.push({ title: "退费金额", content: result ? storeDesc : storeDesc });
  return arr;
}

function _getPOSOrderInfoSection1Arr(result) {
  let arr = [];
  arr.push({ title: "交易金额(元)", content: result && result["amount"] ? "+" + umfUtils.fentoyuan(result["amount"]) : "" });
  return arr;
}

function _getPOSOrderInfoSection2Arr(result) {
  let arr = [];
  arr.push({ title: "交易时间", content: result && result["orderDate"] ? result["orderDate"] : "" });
  arr.push({ title: "流水", content: result && result["tradeNo"] ? result["tradeNo"] : "" });
  arr.push({ title: "终端号", content: result && result["terminalId"] ? result["terminalId"] : "" });
  arr.push({ title: "外部商户号", content: result && result["wMerchantId"] ? result["wMerchantId"] : "" });
  arr.push({ title: "商户订单号", content: result && result["orderNo"] ? result["orderNo"] : "" });
  arr.push({ title: "卡号", content: result && result["cardNo"] ? result["cardNo"] : "" });
  arr.push({ title: "卡种", content: result && result["cardType"] ? result["cardType"] : "" });
  arr.push({ title: "参考号", content: result && result["tranRrn"] ? result["tranRrn"] : "" });
  arr.push({ title: "交易类型", content: result && result["tranType"] ? result["tranType"] : "" }); 
  result && result["refundDate"] &&arr.push({ title: "退费申请日期", content: PayAllMaps[result["refundDate"]]});
  arr.push({ title: "交易状态", content: result && result["tranState"] ? result["tranState"] : "" });
  
  return arr;
}